const userList = [
  {
    userId: 1,
    avatar: 'https://pic1.zhimg.com/80/v2-083faf550543c1e9f134b56b3322ee3c_720w.webp',
    username: 'admin',
    nickname: '超级管理员',
    password: 'admin123',
    desc: '好久不见',
    roles: ['admin'],
    permissions: ['*:*:*'],
    token: 'admin_token',
  },
  {
    userId: 2,
    avatar: 'https://pic1.zhimg.com/80/v2-e1427f6a21122ac163ff98d24f55d372_720w.webp',
    username: 'master',
    nickname: '管理员',
    password: 'master123',
    desc: '不说再见',
    roles: ['master'],
    permissions: ['system:user:query'],
    token: 'master_token',
  },
]

// API 模拟接口
export const apiUser = [
  // 用户登录接口
  {
    url: '/api/user/login',
    method: 'post',
    response: (request) => {
      const { username, password } = request.body

      // 调用获取用户信息函数，用于判断是否有此用户
      const checkUser = userList.find((item) => item.username === username && item.password === password)

      // 失败
      if (!checkUser) {
        return {
          code: 400,
          msg: '账号或者密码不正确',
        }
      }

      // 成功
      return {
        code: 200,
        token: checkUser.token,
        msg: '登录成功',
      }
    },
  },

  // 获取用户信息接口
  {
    url: '/api/user/info',
    method: 'get',
    response: (request) => {
      // 获取请求头携带的 token
      const authorization = request.headers['authorization']
      const token = authorization?.split(' ')[1] // Bearer token 截取

      // 查看用户信息数据中是否包含有此 token 的用户
      const checkUser = userList.find((item) => item.token === token)

      // 失败
      if (!checkUser) {
        return {
          code: 401,
          data: null,
          msg: '用户信息不存在',
        }
      }

      // 成功
      return {
        code: 200,
        data: checkUser,
        msg: '获取用户信息成功',
      }
    },
  },
]
